#include <conio.h>
#include "classinventory.h"
#include "classmenu.h"


inventory::inventory()
{
    money = 0;
    invent[0].kodebarang = 0;
    invent[0].amount = 0;
    invent[1].kodebarang = 0;
    invent[1].amount = 0;
    invent[2].kodebarang = 0;
    invent[2].amount = 0;
    Price[1] = 500;     // bibit ChronoPlant
    Price[2] = 650;     // bibit ReynFruit
    Price[3] = 600;     // bibit Ichalmond
    Price[4] = 800;     // bibit Edondong
    Price[5] = 700;     // bibit GreenAlgoe
    Price[6] = 600;     // bibit Hanselnut
    Price[7] = 1000;    // bibit AbelTree
    Price[8] = 5000;    // bibit Elmonade
    Price[9] = 850;     // buah ChronoPlant
    Price[10] = 1100;    // buah ReynFruit
    Price[11] = 1000;    // buah Ichalmond
    Price[12] = 1300;    // buah Edondong
    Price[13] = 950;     // buah GreenAlgoe
    Price[14] = 1000;    // buah Hanselnut
    Price[15] = 2500;    // buah AbelTree
    Price[16] = 5000;    // buah Elmonade
}

inventory::~inventory()
{

}

void inventory::PrintInventory()
{
    int a;
    char c = 143;
    cout << "INVENTORY" << endl;
    cout << "Money: " << money << " " << c << endl;
    //1 : ChronoPlant,2 : ReynFruit, 3 : Ichalmond, 4: Edondong, 5: GreenAlgoe,
    //6: Hanselnut, 7: AbelTree, 8: Elmonade
    for(a = 0; a < 3; a++)
    {
        cout << "slot " << a << ":" << endl;
        if ((invent[a].kodebarang == 1) || (invent[a].kodebarang == 9))
        {
            cout << "ChronoPlant";
        }
        else if ((invent[a].kodebarang == 2) || (invent[a].kodebarang == 10))
        {
            cout << "ReynFruit";
        }
        else if ((invent[a].kodebarang == 3) || (invent[a].kodebarang == 11))
        {
            cout << "Ichalmond";
        }
        else if ((invent[a].kodebarang == 4) || (invent[a].kodebarang == 12))
        {
            cout << "Edondong";
        }
        else if ((invent[a].kodebarang == 5) || (invent[a].kodebarang == 13))
        {
            cout << "GreenAlgoe";
        }
        else if ((invent[a].kodebarang == 6) || (invent[a].kodebarang == 14))
        {
            cout << "Hanselnut";
        }
        else if ((invent[a].kodebarang == 7) || (invent[a].kodebarang == 15))
        {
            cout << "AbelTree";
        }
        else if ((invent[a].kodebarang == 8) || (invent[a].kodebarang == 16))
        {
            cout << "Elmonade";
        }
        if (IsBibit(a))
            cout << " Seed : ";
        else if (GetKodeBarang(a) != 0)
            cout << " : ";
        cout << invent[a].amount << endl;
    }
    getch();
}

void inventory::InsertInventory(int kodebar, int amo)
{
    int a = 0;
    while ((invent[a].kodebarang != 0)&&(invent[a].kodebarang != kodebar)&&(a <= 2))
    {
        a++;
    }
    if(invent[a].kodebarang == kodebar)
    {
        invent[a].amount = invent[a].amount + amo;
    }
    else if(invent[a].kodebarang == 0)
    {
        invent[a].kodebarang = kodebar;
        invent[a].amount = amo;
    }
    else cout << "Inventory Is Full" << endl;
}

void inventory::DeleteInventory(int kodebar, int amo)
{
    int a = 0;
    while ((invent[a].kodebarang != kodebar)&&(a <= 2))
    {
        a++;
    }
    if(invent[a].kodebarang == kodebar)
    {
        if(invent[a].amount <= amo)
        {
            invent[a].kodebarang = 0;
            invent[a].amount = 0;
        }
        else invent[a].amount = invent[a].amount - amo;
    }
    else cout << "Nothing to Delete, There's No Specified Item" << endl;
}

void inventory::ChangeMoney(long int inmoney)
{
    money = money + inmoney;
}

void inventory::SetMoney(long int inmoney)
{
    money = inmoney;
}

int inventory::GetKodeBarang(int slot)
{
    return(invent[slot].kodebarang);
}

item inventory::GetInvent(int slot)
{
    return(invent[slot]);
}

int inventory::GetMoney()
{
    return(money);
}

int inventory::GetPrice(int kodebar)
{
    return(Price[kodebar]);
}


int inventory::IsBibit(int slot)
{
    return((invent[slot].kodebarang == 1) || (invent[slot].kodebarang == 2) || (invent[slot].kodebarang == 3) || (invent[slot].kodebarang == 4) || (invent[slot].kodebarang == 5) || (invent[slot].kodebarang == 6) || (invent[slot].kodebarang == 7) || (invent[slot].kodebarang == 8));
}

void inventory::Buy(int kodebar, int amo)
{
    if (GetMoney() >= (Price[kodebar] * amo))
    {
    ChangeMoney(-(Price[kodebar]) * amo);
    InsertInventory(kodebar, amo);
    cout << "Transaction Successful" << endl;
    M.IsSaved = 0;
    }
    else
    {
        cout << "Not enough money!" << endl;
    }
}


void inventory::Sell(int slot, int amo)
{
    if (!IsBibit(slot))
        ChangeMoney(Price[GetKodeBarang(slot)] * amo);
    else
        ChangeMoney((Price[GetKodeBarang(slot)] / 2) * amo);
    DeleteInventory(GetKodeBarang(slot), amo);
    M.IsSaved = 0;
}

void inventory::Diskon50()
{
    Price[1] = 250;     // bibit ChronoPlant
    Price[2] = 325;     // bibit ReynFruit
    Price[3] = 300;     // bibit Ichalmond
    Price[4] = 400;     // bibit Edondong
    Price[5] = 350;     // bibit GreenAlgoe
    Price[6] = 300;     // bibit Hanselnut
    Price[7] = 500;    // bibit AbelTree
    Price[8] = 2500;    // bibit Elmonade

}

void inventory::MoreExpensive()
{
    Price[9] = 1020;     // buah ChronoPlant
    Price[10] = 1320;    // buah ReynFruit
    Price[11] = 1200;    // buah Ichalmond
    Price[12] = 1560;    // buah Edondong
    Price[13] = 1140;     // buah GreenAlgoe
    Price[14] = 1200;    // buah Hanselnut
    Price[15] = 3000;    // buah AbelTree
    Price[16] = 6000;    // buah Elmonade
}

void inventory::HargaNetral()
{
    Price[1] = 500;     // bibit ChronoPlant
    Price[2] = 650;     // bibit ReynFruit
    Price[3] = 600;     // bibit Ichalmond
    Price[4] = 800;     // bibit Edondong
    Price[5] = 700;     // bibit GreenAlgoe
    Price[6] = 600;     // bibit Hanselnut
    Price[7] = 1000;    // bibit AbelTree
    Price[8] = 5000;    // bibit Elmonade
    Price[9] = 850;     // buah ChronoPlant
    Price[10] = 1100;    // buah ReynFruit
    Price[11] = 1000;    // buah Ichalmond
    Price[12] = 1300;    // buah Edondong
    Price[13] = 950;     // buah GreenAlgoe
    Price[14] = 1000;    // buah Hanselnut
    Price[15] = 2500;    // buah AbelTree
    Price[16] = 5000;    // buah Elmonade
}

/*
int inventory::showharga()
{
    return Price[1];
}
*/